package com.cat.dataStructure03;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/make-number-of-distinct-characters-equal/
 * @create 2025/11/15 20:16
 * @since JDK17
 */

public class Solution62 {
    void f(int[] h, char[] w) {
        for (var x : w) {
            h[x - 'a']++;
        }
    }
    void swap(int[]h1, int[] h2, int x, int y) {
        h1[x]--;
        h1[y]++;
        h2[y]--;
        h2[x]++;
    }
    public boolean isItPossible(String word1, String word2) {
        int[] h1 = new int[26], h2 = new int[26];
        f(h1, word1.toCharArray());
        f(h2, word2.toCharArray());
        for (int i = 0; i < 26; i++) {
            for (int j = 0; j < 26; j++) {
                if (h1[i] == 0 || h2[j] == 0) {
                    continue;
                }
                swap(h1, h2, i, j);
                int cnt1 = 0, cnt2 = 0;
                for (int k = 0; k < 26; k++) {
                    cnt1 += h1[k] > 0 ? 1 : 0;
                    cnt2 += h2[k] > 0 ? 1 : 0;
                }
                swap(h1, h2, j, i);
                if (cnt1 == cnt2) {
                    return true;
                }
            }
        }

        return false;
    }
}
